import { defineStore } from "pinia";

export const useAddAssStore = defineStore({
  id: "add",
  state: () => ({
    cpu_index: "-1",
    gpu_index: "-1",
    mainboard_index: "-1",
    memory_index: "-1",
    ssd_index: "-1",
    hdd_index: "-1",
    cpu_Info: {},
    gpu_Info: {},
    mainboard_Info: {},
    memory_Info: {},
    ssd_Info: {},
    hdd_Info: {},
    total: 0,
    score: 0,
  }),
  actions: {
    setCpuInfo(cpu_Info: any, cpu_index: string) {
      this.cpu_index = cpu_index;
      this.cpu_Info = cpu_Info;
    },
    setGpuInfo(gpu_Info: any, gpu_index: string) {
      this.gpu_index = gpu_index;
      this.gpu_Info = gpu_Info;
    },
    setMainboardInfo(mainboard_Info: any, mainboard_index: string) {
      this.mainboard_index = mainboard_index;
      this.mainboard_Info = mainboard_Info;
    },
    setMemoryInfo(memory_Info: any, memory_index: string) {
      this.memory_index = memory_index;
      this.memory_Info = memory_Info;
    },
    setSsdInfo(ssd_Info: any, ssd_index: string) {
      this.ssd_index = ssd_index;
      this.ssd_Info = ssd_Info;
    },
    setHddInfo(hdd_Info: any, hdd_index: string) {
      this.hdd_index = hdd_index;
      this.hdd_Info = hdd_Info;
    },
    setTotalPrice(priceState: number) {
      this.priceState = priceState;
    },

    //计算总价
    setTotal() {
      this.total = 0;
      const typeHandlers = {
        CPU: this.cpu_Info,
        GPU: this.gpu_Info,
        主板: this.mainboard_Info,
        内存: this.memory_Info,
        固态硬盘: this.ssd_Info,
        硬盘: this.hdd_Info,
      };
      for (const key in typeHandlers) {
        if (typeHandlers[key].price) {
          this.total += typeHandlers[key].price;
        }
      }
    },

    //计算性能
    setScore() {
      this.score = 0;
      const typeHandlers = {
        CPU: this.cpu_Info,
        GPU: this.gpu_Info,
        主板: this.mainboard_Info,
        内存: this.memory_Info,
        固态硬盘: this.ssd_Info,
        硬盘: this.hdd_Info,
      };
      for (const key in typeHandlers) {
        if (typeHandlers[key].score) {
          //忽略精度问题

          this.score += typeHandlers[key].score;
          this.score = parseFloat(this.score.toFixed(1));
        }
      }
    },

    //重置
    reset() {
      this.cpu_index = "-1";
      this.gpu_index = "-1";
      this.mainboard_index = "-1";
      this.memory_index = "-1";
      this.ssd_index = "-1";
      this.hdd_index = "-1";
      this.cpu_Info = {};
      this.gpu_Info = {};
      this.mainboard_Info = {};
      this.memory_Info = {};
      this.ssd_Info = {};
      this.hdd_Info = {};
      this.total = 0;
    },
  },
});
